/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.member.service.impl;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.wealth.member.entity.Member;
import com.wealth.member.entity.Returncash;
import com.wealth.member.repository.ReturncashDao;
import com.wealth.member.service.IReturncashService;
import com.wealth.system.entity.Corporation;
import com.wealth.wechat.entity.WechatConfig;
import com.wealth.wechat.service.WechatConfigService;

import iaas.enums.StatusEnum;
import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;

/**
 * @author ruanzh
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class ReturncashServiceImpl extends BaseServiceImpl<Returncash> implements IReturncashService{

	@Autowired
	private ReturncashDao returncashDao;
	@Autowired
	private WechatConfigService configService;
    /**
     * 保存好评返现上传的图片
     */
	@Override
	public boolean savePic(Long memberid, String openid,String pic,Long corpid) {
		
		try {
			Returncash rc=new Returncash();
			rc.setCrateTime(new Date());
			rc.setPicture(pic);
			rc.setStatus(StatusEnum.NORMAL.getValue());
			rc.setCorp(new Corporation(corpid));
			rc.setMember(new Member(memberid));
			rc.setOpenid(openid);
			returncashDao.save(rc);
			return false;
		} catch (Exception e) {
			return true;
		}
	}
	
	@Override
	public HSSFWorkbook exportFile(Long corpid,Integer status,String startTime,String endTime) {
		try { 
			//创建wb
			HSSFWorkbook wb = new HSSFWorkbook();    
	        HSSFSheet sheet = wb.createSheet("Account");  
	        
	        //step1.微信appid
			WechatConfig config = configService.getWechatConfigByCorpid(corpid);
			String appid = config.getWxappid();
	        Row row = sheet.createRow(0);
	        row.createCell(0).setCellValue(appid);  
	        
	        //step2 遍历openid
	        Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
	        filters.add(new SearchFilter("status", Operator.EQ, status));
	        filters.add(new SearchFilter("crateTime", Operator.GE, startTime));
	        filters.add(new SearchFilter("crateTime", Operator.LE, endTime));
	        Specification<Returncash> spec = DynamicSpecifications.bySearchFilter(filters, Returncash.class);
	        List<Returncash> dblist=returncashDao.findAll(spec);
	        for (int i = 0; i < dblist.size(); i++) {
	        	Returncash returncash = dblist.get(i);
	            row = sheet.createRow(i+1);    
	            row.createCell(0).setCellValue(returncash.getOpenid());    
	        }
	        wb.close();
	        return wb;
	    }catch (Exception e){  
            e.printStackTrace(); 
            return null;
        }  
		
	}
	/**
	 * 生产文件
	 * @param wb
	 * @return
	 * @throws IOException
	 */
	public boolean timefile(HSSFWorkbook wb) throws IOException{
		
		try {
			Date nowTime=new Date(); 
	    	SimpleDateFormat time=new SimpleDateFormat("yyyyMMddHHmmss"); 
	    	System.out.println(time.format(nowTime)); 
	        FileOutputStream fout;
			fout = new FileOutputStream("E:/"+time.format(nowTime)+".xls");
			wb.write(fout);  
	        fout.close();
	        return false;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return true;
		} 
        
		
		
	}
}
